<?php ob_start(); ?>
<?php 
	/*
	Plugin Name: Nii Watermark
	Plugin URI: http://nii.vn/
	Description: Create watermark when you upload images
	Version: 0.1
	Author: Nii
	Author URI: http://nii.com/
	*/

define('DIR', dirname(__FILE__));


add_filter('wp_generate_attachment_metadata', 'applyWatermark');
function applyWatermark($image){

	$upload_dir = wp_upload_dir();
	//upload file to server
	$filePath = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . $image['file'];
	
	$watermark_path = DIR . DIRECTORY_SEPARATOR . 'logo.png';
	$watermark_id = imagecreatefrompng($watermark_path);
	imagealphablending($watermark_id, false);
	imagesavealpha($watermark_id, true);
	
	$info_img = getimagesize($filePath);
	$info_wtm = getimagesize($watermark_path);
	$fileType = strtolower($info_img['mime']);
	
	$image_w 		= $image['width'];
	$image_h 		= $image['height'];
	$watermark_w	= $info_wtm[0];
	$watermark_h	= $info_wtm[1];
	$is_gif = false;	
	switch($fileType)
	{
		case	'image/gif':	$is_gif = true;break;
		case	'image/png': 	$image_id = imagecreatefrompng($filePath);	break;
		default:				$image_id = imagecreatefromjpeg($filePath);	break;
	}

	if(!$is_gif){
		/* Watermark in the bottom right of image*/
		$dest_x  = ($image_w - $watermark_w) - 20; 
		$dest_y  = ($image_h  - $watermark_h) - 30;
		
		// Watermark in the middle of image 
		// $dest_x = round(( $image_height / 2 ) - ( $logo_h / 2 ));
		// $dest_y = round(( $image_w / 2 ) - ( $logo_w / 2 ));
		
		imagecopy($image_id, $watermark_id, $dest_x, $dest_y, 0, 0, $watermark_w, $watermark_h);
	
		//override to image
		switch($fileType)
		{
			case	'image/png': 	imagepng ($image_id, $filePath); 		break;
			default:				imagejpeg($image_id, $filePath); 		break;
		}      
		imagedestroy($image_id);
		imagedestroy($watermark_id);
	}
}	


// add_filter('wp_handle_upload_prefilter', 'nii_watermark_maker', 1, 1);
// tien xu ly
function nii_watermark_maker($image)
{
	// return $image;
	$size = @getimagesize($image['tmp_name']);
	
	//upload file to server
	$filePath = DIR.'/temp/'.time().'-'. $image['name'];
	
	move_uploaded_file($image['tmp_name'], $filePath);


	//watermark
	if(($size[0] > 150) && ($size[1] > 35)){
		$watermark_path = DIR.'/logo.png';
		$watermark_id = imagecreatefrompng($watermark_path);
		imagealphablending($watermark_id, false);
		imagesavealpha($watermark_id, true);
		
		$info_img = getimagesize($filePath);
		$info_wtm = getimagesize($watermark_path);
		$fileType = strtolower($info_img['mime']);
		
		$image_w 		= $info_img[0];
		$image_h 		= $info_img[1];
		$watermark_w	= $info_wtm[0];
		$watermark_h	= $info_wtm[1];
		$is_gif = false;	
		switch($fileType)
		{
			case	'image/gif':	$is_gif = true;break;
			case	'image/png': 	$image_id = imagecreatefrompng($filePath);	break;
			default:				$image_id = imagecreatefromjpeg($filePath);	break;
		}

		if(!$is_gif && $image_h > 255){
			/* Watermark in the bottom right of image*/
			$dest_x  = ($image_w - $watermark_w) - 20; 
			$dest_y  = ($image_h  - $watermark_h) - 30;
			
			/* Watermark in the middle of image 
			$dest_x = round(( $image_height / 2 ) - ( $logo_h / 2 ));
			$dest_y = round(( $image_w / 2 ) - ( $logo_w / 2 ));
			*/
			imagecopy($image_id, $watermark_id, $dest_x, $dest_y, 0, 0, $watermark_w, $watermark_h);
			
			//override to image
			switch($fileType)
			{
				case	'image/png': 	imagepng ($image_id, $filePath); 		break;
				default:				imagejpeg($image_id, $filePath); 		break;
			}      

			// return $image_id;

			imagedestroy($image_id);
			imagedestroy($watermark_id);
		}
	}

	// end
 	$image = imagecreatefromjpeg($filePath);

 	var_dump($image);

	return $image;
	// return $image;
}
?>